from wormbane.model.feature import Door

import random

def command_open(game, ui):
    cells = [cell for cell in game.hero.env.neighbors
             if cell.feature.is_door
             and not cell.feature.is_open]
    if not cells:
        game.write('There is nothing to open here.')
        return
    cell = random.choice(cells)
    if cell.feature.locked:
        game.write('The door is locked.')
        return
    game.write(game.hero, 'opens', cell.feature)
    cell.feature.state = 'open'
    game.hero_moved = True
